package com.example.capsule_chat0322.adapter;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.bean.FriendApplyBean;
import com.example.capsule_chat0322.configuration.Constant;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.global.LRUCache;
import com.example.capsule_chat0322.interfaces.FriendApplyItemOnClickInterface;
import com.example.capsule_chat0322.request.UserInfoRequest;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/25 21:21
 */
public class FriendApplyAdapter extends RecyclerView.Adapter<FriendApplyAdapter.ViewHold> implements View.OnClickListener {

    public List<FriendApplyBean> friendApplyBeanList;
    private FriendApplyItemOnClickInterface friendApplyItemOnClickInterface;
    private RecyclerView rvParent;

    public void setFunctionItemOnClickInterface(FriendApplyItemOnClickInterface friendApplyItemOnClickInterface) {
        this.friendApplyItemOnClickInterface = friendApplyItemOnClickInterface;
    }


    static class ViewHold extends RecyclerView.ViewHolder {

        LinearLayout friend_item;
        ImageView friend_avatar;
        TextView friend_info;

        public ViewHold(@NonNull View itemView) {
            super(itemView);
            friend_item = itemView.findViewById(R.id.friend_item);
            friend_avatar = itemView.findViewById(R.id.friend_avatar);
            friend_info = itemView.findViewById(R.id.friend_info);
        }
    }


    public FriendApplyAdapter(List<FriendApplyBean> friendApplyBeanList) {
        this.friendApplyBeanList = friendApplyBeanList;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        rvParent = (RecyclerView) parent;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_apply_item, parent, false);
        view.setOnClickListener(this);
        return new FriendApplyAdapter.ViewHold(view);
    }

    @SuppressLint("NewApi")
    @Override
    public void onBindViewHolder(@NonNull ViewHold holder, int position) {

        FriendApplyBean friendApplyBean = friendApplyBeanList.get(position);
        holder.friend_item.setVisibility(View.VISIBLE);

        String applyStatus = friendApplyBean.getApplyStatus();

        if (applyStatus == null) {
            applyStatus = "";
        } else if (applyStatus.equals("0")) {
            applyStatus = "(" + "已拒绝" + ")";
        } else if (applyStatus.equals("1")) {
            applyStatus = "(" + "已同意" + ")";
        }

        String content = "申请人ID: " + friendApplyBean.getApplyId() + applyStatus + "\n" + "申请说明: " + friendApplyBean.getApplyInstruction();
        holder.friend_info.setText(content);

        LRUCache<String, Bitmap> lruCache = (LRUCache<String, Bitmap>) GlobalAssistMap.getObject("lruCache");

        if (friendApplyBean.getFriendAvatarPath().contains("/")) {
            String cacheKey = friendApplyBean.getFriendAvatarPath().substring(friendApplyBean.getFriendAvatarPath().lastIndexOf("/") + 1);
            Bitmap bitmap = lruCache.get(cacheKey);
            if (bitmap == null) {
                UserInfoRequest.getAvatar(new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(@NonNull Message message) {
                        String path = Constant.IMAGE_PATH + cacheKey;
                        Bitmap bitmap = BitmapFactory.decodeFile(path);
                        holder.friend_avatar.setImageBitmap(bitmap);
                        lruCache.put(cacheKey, bitmap);
                        return true;
                    }
                }), friendApplyBean.getFriendAvatarPath());
            } else {
                holder.friend_avatar.setImageBitmap(bitmap);
            }
        } else {
            holder.friend_avatar.setImageResource(Integer.parseInt(friendApplyBean.getFriendAvatarPath()));
        }


    }

    @Override
    public int getItemCount() {
        return friendApplyBeanList.size();
    }


    @Override
    public void onClick(View view) {
        //根据RecyclerView获得当前View的位置
        int position = rvParent.getChildAdapterPosition(view);
        //程序执行到此，会去执行具体实现的onItemClick()方法
        if (friendApplyItemOnClickInterface != null) {
            friendApplyItemOnClickInterface.onItemClick(rvParent, view, position, friendApplyBeanList.get(position));
        }
    }



}
